<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Car;
use DB;



//后台车源列表控制器
class CarController extends Controller
{

    public function car() {

        $car = new Car();
        $carslist = $car::all();
        return view('admin.cars',['carslist'=>$carslist]);
    }

    public function del($pid) {
        $car = new Car();
        $res = $car::find($pid);
        if($res->delete()) {
            echo "删除成功";
        }

    }

    public function carslist() {
        //$car = new Car();
        
        $uri = $_SERVER['REQUEST_URI'];

        $arr = explode('/',$uri);
        unset($arr[0]);
        unset($arr[1]);
        unset($arr[2]);
        // var_dump($arr);exit;


        $con = [];
        $con[] = '1';

        if(in_array('s1', $arr)) {
            //$con['issell'] = 0;
            $con['issell'] ='issell = 0';        
        }
        if(in_array('s2',$arr)) {
            //$con['issell'] = 1;
            $con['issell'] ='issell = 1';            
        }
        if(in_array('s0',$arr)) {
            unset($con['issell']);
        }
        if(in_array('t1',$arr)) {
            $con['issprecom'] = 'issprecom = 1';
        }
        if(in_array('t2',$arr)) {
            $con['isrecom'] = 'isrecom = 1';
        }
        if(in_array('t3',$arr)) {
            $con['ishot'] = 'ishot = 1';
        }
        if(in_array('t0',$arr)) {
            unset($con['issprecom']);
            unset($con['isrecom']);
            unset($con['ishot']);
        }

        //车型
        if(in_array('m0',$arr)) {
            unset($con['p_model']);
        }
        if(in_array('m1',$arr)) {
            $con['p_model'] = 'p_model = 1';
        }
        if(in_array('m2',$arr)) {
            $con['p_model'] = 'p_model = 2';
        }
        if(in_array('m3',$arr)) {
            $con['p_model'] = 'p_model = 3';
        }
        if(in_array('m4',$arr)) {
            $con['p_model'] = 'p_model = 4';
        }
        if(in_array('m5',$arr)) {
            $con['p_model'] = 'p_model = 5';
        }
        if(in_array('m6',$arr)) {
            $con['p_model'] = 'p_model = 6';
        }
        if(in_array('m7',$arr)) {
            $con['p_model'] = 'p_model = 7';
        }
        if(in_array('m8',$arr)) {
            $con['p_model'] = 'p_model = 8';
        }
        if(in_array('m9',$arr)) {
            $con['p_model'] = 'p_model = 9';
        }
        if(in_array('m10',$arr)) {
            $con['p_model'] = 'p_model = 10';
        }
        if(in_array('m11',$arr)) {
            $con['p_model'] = 'p_model = 11';
        }

        //价格
        if(in_array('p0',$arr)) {
            unset($con['p_price']);
        }
        if(in_array('p1',$arr)) {
            $con['p_price'] = 'p_price <=3';
        }
        if(in_array('p2',$arr)) {
            $con['p_price'] = 'p_price >3 and p_price <=5';
        }
        if(in_array('p3',$arr)) {
            $con['p_price'] = 'p_price >5 and p_price <=8';
        }
        if(in_array('p4',$arr)) {
            $con['p_price'] = 'p_price >8 and p_price <=12';
        }
        if(in_array('p5',$arr)) {
            $con['p_price'] = 'p_price >12 and p_price <=18';
        }
        if(in_array('p6',$arr)) {
            $con['p_price'] = 'p_price >18 and p_price <=24';
        }
        if(in_array('p7',$arr)) {
            $con['p_price'] = 'p_price >24 and p_price <=35';
        }
        if(in_array('p8',$arr)) {
            $con['p_price'] = 'p_price >35 and p_price <=50';
        }
        if(in_array('p9',$arr)) {
            $con['p_price'] = 'p_price >50 and p_price <=100';
        }
        if(in_array('p10',$arr)) {
            $con['p_price'] = 'p_price >100';
        }

        //车龄
        if(in_array('y0',$arr)) {
            unset($con['p_year']);
        }
        if(in_array('y1',$arr)) {
            $con['p_year'] = 'p_year = 2016';
        }
        if(in_array('y2',$arr)) {
            $con['p_year'] = 'p_year = 2015';
        }
        if(in_array('y3',$arr)) {
            $con['p_year'] = 'p_year = 2014';
        }
        if(in_array('y4',$arr)) {
            $con['p_year'] = 'p_year = 2013';
        }
        if(in_array('y5',$arr)) {
            $con['p_year'] = 'p_year = 2012';
        }
        if(in_array('y6',$arr)) {
            $con['p_year'] = 'p_year = 2011';
        }
        if(in_array('y7',$arr)) {
            $con['p_year'] = 'p_year = 2010';
        }

        //排量
        if(in_array('g0',$arr)) {
            unset($con['p_gas']);
        }
        if(in_array('g1',$arr)) {
            $con['p_gas'] = 'p_gas >0 and p_gas <2';
        }
        if(in_array('g2',$arr)) {
            $con['p_gas'] = 'p_gas >=2 and p_gas <3';
        }
        if(in_array('g3',$arr)) {
            $con['p_gas'] = 'p_gas >=3 and p_gas <4';
        }
        if(in_array('g4',$arr)) {
            $con['p_gas'] = 'p_gas >=4 and p_gas <5';
        }
        if(in_array('g5',$arr)) {
            $con['p_gas'] = 'p_gas >=5';
        }
        
        $where = implode(' and ',$con);

        //$carslist = $car::where($con)->get();
        $carslist = DB::select("select * from cars where $where");
        return view('admin.cars',['carslist'=>$carslist,'uri'=>$uri]);
    }
}
